﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;

namespace SupplyChainManagement
{
    /// <summary>
    /// Form to select which of the raw items you wish to order
    /// </summary>
    public partial class OrderInventory : Form
    {
        List<string> rawItemData = new List<string>();
        InventoryForm invenForm;
        List<string> selectedItems = new List<string>();

        public OrderInventory(InventoryForm form)
        {
            invenForm = form;
            InitializeComponent();
            LoadSelections();
        }

        /// <summary>
        /// Load the selections for the listbox for raw items
        /// </summary>
        private void LoadSelections()
        {
            OdbcConnection dbMySQL = new OdbcConnection(MainForm.strConnect);
            try
            {
                //open database
                dbMySQL.Open();

                OdbcCommand sqlCommand = dbMySQL.CreateCommand();
                //assign the SQL command
                sqlCommand.CommandText = "select inv_name from inventory";
                //reader executes the select command
                OdbcDataReader sqlReader = sqlCommand.ExecuteReader();

                while (sqlReader.Read())
                {
                    rawItemData.Add(sqlReader.GetString(0));
                }

                //close readers and dispose the commands
                sqlReader.Close();
                sqlCommand.Dispose();
                //adds to the listbox
                rawItemListBox.Items.AddRange(rawItemData.ToArray());
            }
            catch (OdbcException ex)
            {
                Console.WriteLine("Database error\n\n{0}", ex.ToString());
            }
            finally
            {
                if (dbMySQL != null) dbMySQL.Close();
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            selectedItems.Clear();
            foreach (string s in rawItemListBox.SelectedItems)
            {
                selectedItems.Add(s.ToString());
            }
            OrderInventoryNext next = new OrderInventoryNext(selectedItems, this, invenForm);
            next.Show();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
            invenForm.Enabled = true;
            invenForm.Activate();
        }

        private void OrderInventory_FormClosed(object sender, FormClosedEventArgs e)
        {
            invenForm.Enabled = true;
            invenForm.Activate();
        }
    }
}
